---
title: "Untitled"
output: html_document
---

############################################
Title: 4_02_Table_2.rmd
Purpose: This code runs baseline forecasting regression in Table 2
Last updated: 18-04-2023

Input:
    - Baseline macro data. Latest version: final_agg_macro_data_monthly.csv 

Output:
    - Table 2

##########################################



########################################## 
1. Setup
########################################## 

0. Setup *Set your file path here*
```{r, results="hide", include=FALSE}
#Clear user defined environment
rm(list=ls())

#Load packages
require(stargazer); require(lubridate); library(sandwich); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(lmtest); library(psych)
require(quantmod); require(dynlm); require(AER); require(vars); require(forecast); require(stargazer); require(strucchange); require(xts); require(lubridate); require(fGarch); require(zoo); require(timeSeries); library(sandwich); require(car);library(fBasics); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(QuantPsyc); library(tseries); library(latticeExtra); library(corrplot); library(psych) ; library(stringr); library(readr); library(see)

#options
options(digits=5)
options(stringsAsFactors = FALSE)
options(scipen=999)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise
list <- base::list

#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"


```

1.1 Load data 
```{r, results="hide", include=FALSE}

final_agg_macro_data <- read_csv(paste0(global, "/Data/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))

```

1.2 Select sample period to use
```{r, results="hide", include=FALSE}

#Select sample period
macrofin_m_2000_extd = subset(final_agg_macro_data, date > "1999-11-01" & date < "2023-04-01")  

#Exclude GFC
macrofin_m_2000_extd_excrisis = subset(macrofin_m_2000_extd, date < "2007-12-01" | date > "2009-03-01") 

```



########################################## 
2. Table 2 Results
########################################## 

2.1 OLS Reg: Panel A
```{r, include=FALSE, results='hide'}


mod_base = lm(lip_3m_ahead ~   termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_base = NeweyWest(lm(lip_3m_ahead ~   termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_base, vcov = nw_mod_base)
robust_mod_base = coeftest(mod_base, vcov = nw_mod_base)[,3]
coef_mod_base = lm.beta(mod_base)


#Credit Spread: GZ spread
mod_pe_1 = lm(lip_3m_ahead ~ termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_1 = NeweyWest(lm(lip_3m_ahead ~ termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_1, vcov = nw_mod_pe_1 )
robust_mod_pe_1 = coeftest(mod_pe_1, vcov = nw_mod_pe_1)[,3]
coef_mod_pe_1=lm.beta(mod_pe_1)


#Credit Spread: GZ spread
mod_pe_3 = lm(lip_3m_ahead ~ cp_treas3mo_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_3 = NeweyWest(lm(lip_3m_ahead ~ cp_treas3mo_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_3, vcov = nw_mod_pe_3 )
robust_mod_pe_3 = coeftest(mod_pe_3, vcov = nw_mod_pe_3 )[,3]
coef_mod_pe_3=lm.beta(mod_pe_3)


#Credit Spread: Loan spread
mod_pe_5 = lm(lip_3m_ahead ~ baa_aaa_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_5 = NeweyWest(lm(lip_3m_ahead ~ baa_aaa_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_5, vcov = nw_mod_pe_5 )
robust_mod_pe_5 = coeftest(mod_pe_5, vcov = nw_mod_pe_5 )[,3]
coef_mod_pe_5=lm.beta(mod_pe_5)


#Credit Spread: Loan spread
mod_pe_6 = lm(lip_3m_ahead ~ hy_aaa_fd + termspr + ffr_r + lip_1m_behind, data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_6 = NeweyWest(lm(lip_3m_ahead ~ hy_aaa_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_6, vcov = nw_mod_pe_6 )
robust_mod_pe_6 = coeftest(mod_pe_6, vcov = nw_mod_pe_6 )[,3]
coef_mod_pe_6=lm.beta(mod_pe_6)


#Credit Spread: Loan spread
mod_pe_7a = lm(lip_3m_ahead ~ gz_spr_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_7a = NeweyWest(lm(lip_3m_ahead ~ gz_spr_fd + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_7a, vcov = nw_mod_pe_7a )
robust_mod_pe_7a = coeftest(mod_pe_7a, vcov = nw_mod_pe_7a )[,3]
coef_mod_pe_7a=lm.beta(mod_pe_7a)



#Credit Spread: Loan spread
mod_pe_7 = lm(lip_3m_ahead ~ loan_spr_fd + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_7 = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd + termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_7, vcov = nw_mod_pe_7 )
robust_mod_pe_7 = coeftest(mod_pe_7, vcov = nw_mod_pe_7 )[,3]
coef_mod_pe_7=lm.beta(mod_pe_7)




mod_pe_8 = lm(lip_3m_ahead ~ first_pc_fd  + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_8 = NeweyWest(lm(lip_3m_ahead ~ first_pc_fd  + termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_8, vcov = nw_mod_pe_8 )
robust_mod_pe_8 = coeftest(mod_pe_8, vcov = nw_mod_pe_8)[,3]
coef_mod_pe_8=lm.beta(mod_pe_8)




mod_pe_9 = lm(lip_3m_ahead ~ loan_spr_fd + first_pc_fd  + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_pe_9 = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd + first_pc_fd + termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_pe_9, vcov = nw_mod_pe_9 )
robust_mod_pe_9 = coeftest(mod_pe_9, vcov = nw_mod_pe_9)[,3]
coef_mod_pe_9=lm.beta(mod_pe_9)



stargazer(mod_pe_1, mod_pe_3, mod_pe_5, mod_pe_6, mod_pe_7a, mod_pe_7, mod_pe_8, mod_pe_9,
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("Industrial Production - 3m ahead"), 
          se = list(robust_mod_pe_1, robust_mod_pe_3, robust_mod_pe_5, robust_mod_pe_6, robust_mod_pe_7a, robust_mod_pe_7, robust_mod_pe_8, robust_mod_pe_9), 
          coef = list(coef_mod_pe_1, coef_mod_pe_3, coef_mod_pe_5, coef_mod_pe_6, coef_mod_pe_7a, coef_mod_pe_7, coef_mod_pe_8, coef_mod_pe_9),
          omit = c( "Constant", "lip_1m_behind","ism_man_1m_behind", "ism_nonman_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
          )

```

2.2 OLS Reg: Panel B
```{r, results="hide", include=FALSE}


mod_lip = lm(lip_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + lip_1m_behind, data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_lip = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lip, vcov = nw_mod_lip )
robust_mod_lip = coeftest(mod_lip, vcov = nw_mod_lip)[,3]
coef_mod_lip=lm.beta(mod_lip)


mod_ism_man = lm(ism_man_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_ism_man = NeweyWest(lm(ism_man_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_man, vcov = nw_mod_ism_man )
robust_mod_ism_man = coeftest(mod_ism_man, vcov = nw_mod_ism_man)[,3]
coef_mod_ism_man=lm.beta(mod_ism_man)



mod_ism_nonman = lm(ism_nonman_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_ism_nonman = NeweyWest(lm(ism_nonman_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman )
robust_mod_ism_nonman = coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman)[,3]
coef_mod_ism_nonman=lm.beta(mod_ism_nonman)



mod_tcu = lm(tcu_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + tcu_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_tcu = NeweyWest(lm(tcu_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + tcu_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_tcu, vcov = nw_mod_tcu )
robust_mod_tcu = coeftest(mod_tcu, vcov = nw_mod_tcu)[,3]
coef_mod_tcu=lm.beta(mod_tcu)




mod_lnew = lm(lnew_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + lnew_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_lnew = NeweyWest(lm(lnew_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + lnew_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lnew, vcov = nw_mod_lnew )
robust_mod_lnew = coeftest(mod_lnew, vcov = nw_mod_lnew)[,3]
coef_mod_lnew=lm.beta(mod_lnew)




mod_linv = lm(linv_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + linv_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_linv = NeweyWest(lm(linv_3m_ahead ~ loan_spr_fd + first_pc_fd +  termspr + ffr_r  + linv_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_linv, vcov = nw_mod_linv )
robust_mod_linv = coeftest(mod_linv, vcov = nw_mod_linv)[,3]
coef_mod_linv=lm.beta(mod_linv)





stargazer(mod_lip, mod_tcu, mod_lnew, mod_linv, mod_ism_man, mod_ism_nonman,
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("coincident Index - 12m ahead"), 
            se = list(robust_mod_lip, robust_mod_tcu, robust_mod_lnew, robust_mod_linv, robust_mod_ism_man, robust_mod_ism_nonman), 
          coef = list(coef_mod_lip, coef_mod_tcu, coef_mod_lnew, coef_mod_linv, coef_mod_ism_man, coef_mod_ism_nonman),
          omit = c( "Constant", "lip_1m_behind","ism_man_1m_behind", "ism_nonman_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
               )





```



